//
//  NetWorkManager.m
//  QuickCure37
//
//  Created by 马文星 on 16/6/18.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "NetWorkManager.h"
#import "NetWorkTool.h"
#import "NetWorkDefine.h"
#import "Users.h"
#import "Disease.h"
#import "DoctorsIfonModel.h"


@implementation NetWorkManager

/**单例*/
static id _instance;
+(instancetype)sharedManage
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    
    return _instance;
    
}
/**天气接口*/
- (void)loadWeatherWithCurrentCity:(NSString *)currentcity sucessBlock:(void (^)(NSDictionary *))ucessBlock errorBlock:(void (^)(NSString *))errorBlock
{
      NSString *city = currentcity;
    NSString *urlString = @"http://api.map.baidu.com/telematics/v3/weather";
    
    NSDictionary *paramDic = @{@"location":city,@"output":@"json",@"ak":@"17IvzuqmKrK1cGwNL6VQebF9"};
    
     [[NetWorkTool shareManager]GET:urlString parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         
         NSLog(@"%@",responseObject);
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         
     }];
    
}


/**登录接口*/

- (void)loginUserWihtAccount:(NSString *)account password:(NSString *)password successBlock:(void (^)(Users * dto))successBlock errorBlock:(void (^)(NSString *))errorBlock
{
    NSString *urlString = kLoginUrl;
    NSDictionary *paramDic = @{@"login_token":@"985c96ce43bb5ef69de3e1ad8afaa83f"};
    
    [[NetWorkTool shareManager]POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
        NSDictionary *dic = responseObject[@"data"];
        
        Users *user = [Users userWithDic:dic];
        
        successBlock(user);
        
    } failureBlock:^(NSError *error) {
        
        errorBlock(@"登录失败");
        
    }];
    
}

/**肿瘤疾病选择疾病细分列表接口*/
- (void)loadDiseaseSubWithCi_id:(int)ci_id page:(int)page successBlock:(void (^)(NSArray *))successBlock errorBlock:(void (^)(NSString *))errorBlock
{
    NSString *urlString = kSearchCI3Url;
    NSDictionary *paramDic = @{@"page":@(1),@"page_size":@(15),@"ci1_id":@(1),@"keyword":@""};
    
    [[NetWorkTool shareManager]POST:urlString parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *arr = responseObject[@"data"];
        
        NSMutableArray *tempArray = [NSMutableArray array];
        
        for (NSDictionary *dic in arr) {
            
            Disease *disease = [Disease diseaseWithDic:dic];
            
            [tempArray addObject:disease];
        }
        
        successBlock(tempArray);

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"失败");
    }];
}

#pragma
#pragma mark - 4.获取医生信息
- (void)loadDoctorsIfonWithParmas:(NSDictionary *)paramDic completeBlock:(void(^)(NSArray<DoctorsIfonModel *> *modelList,NSError *error))completeBlock errorBlock:(void(^)(NSString *error))errorBlock
{
    NSString *urlString = kMatchDoctors;
    
    [[NetWorkTool shareManager] POST_RequestWithUrlString:urlString parmas:paramDic successBloock:^(id responseObject) {
        
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        NSArray *stringArray = [str componentsSeparatedByString:@"<br />"];
        
        NSData *data = [stringArray[2] dataUsingEncoding:NSUTF8StringEncoding];
        
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        
        NSArray *dictArray = dict[@"data"];
        
        NSArray<DoctorsIfonModel *>* modelList = [NSArray yy_modelArrayWithClass:[DoctorsIfonModel class] json:dictArray];
        
        completeBlock(modelList,nil);
        
        
    } failureBlock:^(NSError *error) {
        
        completeBlock(nil,error);
        
    }];
    
}

















@end
